package com.neusoft.elm.service.impl;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import com.neusoft.elm.dao.CartDao;
import com.neusoft.elm.dao.DeliveryAddressDao;
import com.neusoft.elm.dao.OrderDetailetDao;
import com.neusoft.elm.dao.OrdersDao;
import com.neusoft.elm.dao.impl.CartDaoImpl;
import com.neusoft.elm.dao.impl.DeliveryAddressDaoImpl;
import com.neusoft.elm.dao.impl.OrderDetailetDaoImpl;
import com.neusoft.elm.dao.impl.OrdersDaoImpl;
import com.neusoft.elm.po.Cart;
import com.neusoft.elm.po.DeliveryAddress;
import com.neusoft.elm.po.OrderDetailet;
import com.neusoft.elm.po.Orders;
import com.neusoft.elm.service.OrdersService;
import com.neusoft.elm.util.DBUtil;
public class OrdersServiceImpl implements OrdersService{
	@Override
	public int createOrders(String userId, Integer businessId, Integer daId, BigDecimal orderTotal) {
	    int orderId = 0;
	    
	    CartDao cartDao = new CartDaoImpl();
	    OrdersDao ordersDao = new OrdersDaoImpl();
	    OrderDetailetDao orderDetailetDao = new OrderDetailetDaoImpl();
	    DeliveryAddressDao deliveryAddressDao = new DeliveryAddressDaoImpl();
	    
	    try {
	        DBUtil.beginTransaction(); //开启一个事务
	        
	        //1、查询当前用户购物车中当前商家的所有食品
	        Cart cart = new Cart();
	        cart.setUserId(userId);
	        cart.setBusinessId(businessId);
	        List<Cart> cartList = cartDao.listCart(cart);
	        
	        //2、获取送货地址信息
	        DeliveryAddress deliveryAddress = deliveryAddressDao.getDeliveryAddressById(daId);
	        
	        //3、创建订单，并获取订单编号
	        Orders orders = new Orders();
	        orders.setUserId(userId);
	        orders.setBusinessId(businessId);
	        orders.setDaId(daId);
	        orders.setOrderTotal(orderTotal);
	        //保存送货地址快照
	        orders.setContactName(deliveryAddress.getContactName());
	        orders.setContactSex(deliveryAddress.getContactSex());
	        orders.setContactTel(deliveryAddress.getContactTel());
	        orders.setAddress(deliveryAddress.getAddress());
	        orderId = ordersDao.saveOrders(orders);
	        
	        //4、向订单明细表中批量添加明细数据
	        List<OrderDetailet> orderDetailetList = new ArrayList();
	        for(Cart c : cartList) {
	            OrderDetailet od = new OrderDetailet();
	            od.setOrderId(orderId);
	            od.setFoodId(c.getFoodId());
	            od.setQuantity(c.getQuantity());
	            //保存商品快照
	            od.setFoodName(c.getFood().getFoodName());
	            od.setFoodPrice(c.getFood().getFoodPrice());
	            od.setFoodImg(c.getFood().getFoodImg());
	            od.setFoodExplain(c.getFood().getFoodExplain());
	            orderDetailetList.add(od);
	        }
	        orderDetailetDao.saveOrderDetailetBatch(orderDetailetList);
	        
	        //5、清空购物车
	        cartDao.removeCart(cart);
	        
	        DBUtil.commitTransaction(); //提交一个事务
	    } catch (Exception e) {
	        orderId = 0;
	        try {
	            DBUtil.rollbackTransaction(); //回滚一个事务
	        } catch (Exception e1) {
	            e1.printStackTrace();
	        } 
	        e.printStackTrace();
	    } finally {
	        DBUtil.close();
	    }
	    return orderId;
	}
 @Override
 public Orders getOrdersById(Integer orderId) {
 Orders orders = null;
 
 OrdersDao ordersDao = new OrdersDaoImpl();
 OrderDetailetDao orderDetailetDao = new OrderDetailetDaoImpl();
 
 try {
 DBUtil.getConnection();
 
 //1、根据订单ID查询订单信息（多对一：商家）
 orders = ordersDao.getOrdersById(orderId);
 
 //2、根据订单ID查询订单明细信息
 List<OrderDetailet> list = orderDetailetDao.listOrderDetailetByOrderId(orderId);
 orders.setList(list);
 
 }catch (Exception e) {
	 e.printStackTrace();
 }finally {
 DBUtil.close();
 }
 return orders;
 }
 
 @Override
 public List<Orders> listOrdersByUserId(String userId){
 List<Orders> list = new ArrayList<>();
 
 OrdersDao ordersDao = new OrdersDaoImpl();
 OrderDetailetDao orderDetailetDao = new OrderDetailetDaoImpl();
 
 try {
 DBUtil.getConnection();
 
 //1、根据用户ID查询订单信息（多对一：商家）
 list = ordersDao.listOrdersByUserId(userId);
 
 //2、查询多个订单的订单明细信息
 for(Orders o : list) {
 List<OrderDetailet> odList =
orderDetailetDao.listOrderDetailetByOrderId(o.getOrderId());
 o.setList(odList);
 }
 
 }catch (Exception e) { 
 e.printStackTrace();
 }finally {
 DBUtil.close();
 }
 return list;
 }
}

 
